﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;

namespace DotNetNinjaLibrary.TestApplication
{
	public class MapRenderingItemsControl : ItemsControl
	{
		public MapModel Map
		{
			get { return (MapModel)GetValue(MapProperty); }
			set { SetValue(MapProperty, value); }
		}

		public static readonly DependencyProperty MapProperty =
			DependencyProperty.Register("Map", typeof(MapModel), typeof(MapRenderingItemsControl), null);

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			var itemsPresenter = this.GetTemplateChild("ItemsPanel") as ItemsPresenter;
			if (itemsPresenter == null)
				return;

			itemsPresenter.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

			var itemsPanel = VisualTreeHelper.GetChild(itemsPresenter, 0) as Grid;
			if (itemsPanel == null)
				return;

			for (int i = 0; i < this.Map.Height; i++)
			{
				itemsPanel.RowDefinitions.Add(new RowDefinition { Height = new GridLength(5) });
			}

			for (int i = 0; i < this.Map.Width; i++)
			{
				itemsPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(5) });
			}
		}
	}
}
